package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerMapper;
import com.atguigu.jxc.dao.PurchaseMapper;
import com.atguigu.jxc.dao.SellMapper;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.SellService;
import com.atguigu.jxc.util.DateUtil;
import lombok.SneakyThrows;
import org.omg.CORBA.ObjectHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class SellServiceImpl implements SellService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private SellMapper sellMapper;
    @Autowired
    private PurchaseMapper purchaseMapper;

    @Override
    public List<Customer> getCustomerList(String q) {
        return customerMapper.getList(q);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSaleListGoods(SaleList saleList, List<SaleListGoods> saleListGoods) {
        sellMapper.saveSaleList(saleList);
        SaleList saleList1 = sellMapper.getSaleList(saleList.getSaleNumber());
        for (SaleListGoods saleListGood : saleListGoods) {
            saleListGood.setSaleListId(saleList1.getSaleListId());
        }
        sellMapper.saveSaleListGoods(saleListGoods);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCustomerReturnListGoods(CustomerReturnList customerReturnList, List<CustomerReturnListGoods> list) {
        sellMapper.saveCustomerReturnList(customerReturnList);
        CustomerReturnList returnList = sellMapper.getSCustomerReturnList(customerReturnList.getReturnNumber());
        for (CustomerReturnListGoods customerReturnListGoods : list) {
            customerReturnListGoods.setCustomerReturnListId(returnList.getCustomerReturnListId());
        }
        sellMapper.saveCustomerReturnListGoods(list);
    }

    @Override
    public Map<String, Object> getSaleListGoodsList(String saleNumber, Integer customerId, Integer state, String sTime, String eTime) {
        List<SaleList> list = sellMapper.getSaleListGoodsList(saleNumber, customerId, state, sTime, eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

    @Override
    public Map<String, Object> getCustomerReturnListGoodsList(String returnNumber, Integer customerId, Integer state, String sTime, String eTime) {
        List<CustomerReturnList> list = sellMapper.getCustomerReturnListGoodsList(returnNumber, customerId, state, sTime, eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

    @Override
    public Map<String, Object> customerReturnListGoodsList(Integer customerReturnListId) {
        List<CustomerReturnListGoods> list = sellMapper.customerReturnListGoodsList(customerReturnListId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delCustomerReturnListAndGoods(Integer customerReturnListId) {
        sellMapper.delCustomerReturnGoods(customerReturnListId);
        sellMapper.delCustomerReturnList(customerReturnListId);
    }

    @Override
    public void updateState(Integer saleListId) {
        sellMapper.updateState(saleListId);
    }

    @Override
    public List<Map> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<Map> count = sellMapper.count(sTime, eTime, goodsTypeId, codeOrName);
        return count;
    }

    @Override
    public List<AVGPojo> getSaleDataByDay(String sTime, String eTime) {
        List<AVGPojo> saleDataByDay = sellMapper.getSaleDataByDay(sTime,eTime);
        List<AVGPojo> purchaseDataByDay = sellMapper.getPurchaseDataByDay(sTime,eTime);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, AVGPojo> avgPojos = new TreeMap<>(((Comparator<String>) (o1, o2) -> {
            Long t1 = null, t2 = null;
            try {
                t1 = dateFormat.parse(o1).getTime();
                t2 = dateFormat.parse(o2).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return t1.compareTo(t2);
        }));
        avgPojos = saleDataByDay.stream().collect(Collectors.toMap(AVGPojo::getDate, avgPojo -> avgPojo.initProfit()));
        extracted(purchaseDataByDay, avgPojos);
        return new ArrayList<>(avgPojos.values());
    }

    private void extracted(List<AVGPojo> purchaseDataByDay, Map<String, AVGPojo> avgPojos) {
        for (AVGPojo avgPojo : purchaseDataByDay) {
            if (avgPojos.containsKey(avgPojo.getDate())) {
                AVGPojo avgPojo1 = avgPojos.get(avgPojo.getDate());
                avgPojo1.setPurchasingTotal(avgPojo.getPurchasingTotal());
                avgPojo1.initProfit();
            } else {
                avgPojos.put(avgPojo.getDate(), avgPojo.initProfit());
            }
        }
    }



    @Override
    public List<AVGPojo> getSaleDataByMonth(String sTime, String eTime) {
        List<AVGPojo> saleDataByMonth = sellMapper.getSaleDataByMonth(sTime,eTime);
        List<AVGPojo> purchaseDataByMonth = sellMapper.getPurchaseDataByMonth(sTime,eTime);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Map<String, AVGPojo> avgPojos = new TreeMap<>(((Comparator<String>) (o1, o2) -> {
            Long t1 = null, t2 = null;
            try {
                t1 = dateFormat.parse(o1).getTime();
                t2 = dateFormat.parse(o2).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return t1.compareTo(t2);
        }));
        avgPojos = saleDataByMonth.stream().collect(Collectors.toMap(AVGPojo::getDate, avgPojo -> avgPojo.initProfit()));
        extracted(purchaseDataByMonth, avgPojos);
        return new ArrayList<>(avgPojos.values());
    }

    @Override
    public Map<String, Object> saleGoodsList(Integer saleListId) {
        List<SaleListGoods> list = sellMapper.saleGoodsList(saleListId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delSaleListAndGodos(Integer saleListId) {
        sellMapper.delSaleListGoods(saleListId);
        sellMapper.delSaleList(saleListId);
    }


}
